<?php
apf_require_class('Dao_DAO');

class Dao_Attendance_LeaveRecord extends DAO{
    public function get_table_name(){
        return 'leave_record';
    }

    public function get_read_pdo_name(){
        return 'attendance_slave';
    }

    public function get_write_pdo_name(){
        return 'attendance_master';
    }

    public function get_table_pk(){
        return 'id';
    }

    /**
     * 获取区间内用户的年假总和
     *
     * @param mixed $user_id
     * @param mixed $start
     * @param mixed $end
     * @access public
     * @return void
     */
    public function get_annual_between($user_id,$start,$end) {
        $table = $this->get_table_name();
        $sql = "SELECT sum(leave_time) as leave_time FROM $table WHERE user_id = $user_id AND start_time > '$start' AND start_time < '$end' AND type = '年假'  AND (status = '已批准' or status = '未审核')";
        return $this->execute($sql);
    }

    public function get_leave_record_by_between($uids,$start,$end) {
        $result = array();
        if(empty($uids) || !is_array($uids)) {
            return $result;
        }
        $uids = implode(',', $uids);
        //加条件判断取出128天(产假)上下间的数据 在取出的数据中过虑
        $t_start = strtotime($start) - 128*86400;
        $nstart = date('Y-m-d',$t_start);
        $t_end = strtotime(substr($end,0,10)) + 128*86400;
        $nend = date('Y-m-d',$t_end)." 23";
        $end_apply = date('Y-m-d',$t_end)." 23:59";
        $curr_date = strtotime($start);
        $month_end_day = date('Y-m-d',mktime(0,0,0,date('m',$curr_date)+1,0,date('Y',$curr_date)))." 18:00";
        $sql = "select user_id,apply_time,status,type,leave_time,start_time,end_time,leave_reason from leave_record where user_id in ({$uids}) and start_time >= '{$nstart}' and end_time <= '{$nend}' AND status !='已撤销' AND status != '已驳回'";
        $temp = $this->execute($sql);
        foreach ($temp as $key => $val) {
            //取出$start ,$end 之间需要的数据
            if($start <= $val['start_time'] && $end >= $val['end_time'] && $val['apply_time'] <= $end_apply) {
                //在当月之间数据
                $result[] = $val;
            } elseif($start >= $val['start_time'] && $end >= $val['end_time'] && $start <= $val['end_time'] && $val['apply_time'] <= $end_apply) {
                //请假开始时间少于当月开始时间 && 结束时间少于单月结束时间 && 结束时间少于单月结束时间
                $result[] = $val;
            } elseif ($end >= $val['start_time'] && $end <= $val['end_time'] && $val['apply_time'] <= $end_apply) {
                //请假开始时间少于当月开始时间 && 结束时间大于单月时间 && 少于当月申请时间
                $result[] = $val;
            } elseif($start <= $val['start_time'] && $end <= $val['end_time'] && $val['apply_time'] <= $end_apply) {
                //请假的记录大于当月第一天，少于当月最后一天
                //定制当月开始，结束时间点
                if($val['end_time'] <=$month_end_day){
                    $result[] = $val;
                }
                $val['start_time'] = date('Y-m-d',mktime(0,0,0,date('m',$curr_date),1,date('Y',$curr_date)))." 9:00";;
                $val['end_time'] = $month_end_day;
                //$result[] = $val;
            } elseif ($val['start_time'] >= $start && $val['end_time'] >= $month_end_day  && $val['apply_time'] <= $end_apply) {
                $result[] = $val;
            }
        }
        return $result;
    }
    /**
     * 请假，公出，出差，非撤销，非驳回
     * @param unknown_type $start
     * @param unknown_type $end
     * @param unknown_type $type
     * @param unknown_type $user_id
     * @return Ambigous <multitype:, boolean, NULL>
     */
    public function get_complect_time_leave_record($start,$end,$type,$user_id){
        $sql = "SELECT start_time,end_time,type FROM leave_record WHERE user_id={$user_id} AND status NOT IN('已撤销','已驳回') AND `type` NOT IN('缺勤','迟到','早退','迟到,早退','全天缺勤','上午缺勤','下午缺勤') AND ((start_time <= '{$start}' AND end_time > '{$start}') or (start_time < '{$end}' AND end_time >= '{$end}'))";
        $temp = $this->execute($sql);
        return $temp;
    }
}
